#coding=utf-8
from django.http import HttpResponseRedirect
from DjangoBbs.Common import Permission
from DjangoBbs.Help.VisitPerm import VisitBbsClass
from PublicTool import HttpWrite
from PublicTool.ViewHelper import ViewHelper
import models
__author__ = 'czq'

def addTheme_Extcredit(request,user,type='add'):
        from DjangoBbs.SiteSetting.models import SiteExtcredit
        extcredit1=SiteExtcredit.objects.get(number=1)
        extcredit2=SiteExtcredit.objects.get(number=2)
        #user=request.user
        profile = user.get_profile()
        if type=='add':
            profile.extcredits1+=extcredit1.addTheme
            profile.extcredits2+=extcredit2.addTheme
            #数量
            profile.themeCount+=1
        else:
            profile.extcredits1-=extcredit1.addTheme
            profile.extcredits2-=extcredit2.addTheme
            profile.themeCount-=1
        profile.save()

def addReply_Extcredit(request,user,type='add'):
        from DjangoBbs.SiteSetting.models import SiteExtcredit
        extcredit1=SiteExtcredit.objects.get(number=1)
        extcredit2=SiteExtcredit.objects.get(number=2)
        #user=request.user
        profile = user.get_profile()
        if type=='add':
            profile.extcredits1+=extcredit1.addReply
            profile.extcredits2+=extcredit2.addReply
            profile.replyCount+=1
        else:
            profile.extcredits1-=extcredit1.addReply
            profile.extcredits2-=extcredit2.addReply
            profile.replyCount-=1
        profile.save()

class ThemeViewHelper(ViewHelper):
    def __init__(self):
        pass
    #显示
    def show_get_dict(self,request,instance,dict={}):

        import forms
        #最新的5个回复
        replyList=models.BbsReply.objects_show.filter(theme=instance).order_by("-id")[:5]
        replyForm=forms.bbsReplyForm

        dict['replyList']=replyList
        dict['replyForm']=replyForm
        #dict={'replyList':replyList,'replyForm':replyForm}
        if instance.isVote:
            from models_vote import BbsThemeVote,BbsThemeVoteRecord,BbsThemeVoteAttr
            if request.user.is_authenticated():
                isVote =  BbsThemeVoteRecord.objects.filter(theme=instance,addUser=request.user)[:1]
            else:
                isVote=True
            voteList=instance.bbsthemevote_set.all()
            dict["voteList"]=voteList
            if isVote:
                attr=BbsThemeVoteAttr.objects.get(theme=instance)
                dict["totalCount"]=attr.totalCount

                dict['isVote']=True
                #dict['voteTotalCount']=
            else:
                dict['isVote']=False
        dict["isClassManager"]=Permission.isClassManager(request,instance.classId)
        #if request.user.is_superuser:
        #    dict["isClassManager"]=True
        #else:
        #    adminUsers=instance.classId.adminUsers.all()#版主
        #    for admin in adminUsers:
        #        if request.user==admin:
        #            dict["isClassManager"]=True
        #            break


        return dict
    def show_submit(self,request,instance):
        import forms
        if request.method=="POST":
            replyForm=forms.bbsReplyForm(request.POST)
            if replyForm.is_valid():
                form=replyForm.save(commit=False)
                form.addUser=request.user
                form.theme=instance
                form.classId=instance.classId
                form.save()

                #raise Exception,form.id
                #最后回复
                instance.lastReply=form
                #回复数量+1
                instance.replyCount+=1
                instance.save()
                #板块回复数量+1
                instance.classId.replyCount+=1
                instance.classId.lastReply=form
                instance.classId.save()
                #计算积分
                self.addReply_Extcredit(request)
                return HttpResponseRedirect(request.META['HTTP_REFERER'])
        else:
            #浏览数量+1
            instance.visitCount+=1
            instance.save()
        return
    def show_response(self,request,instance,dict,url='show'):
        if not VisitBbsClass(request.user,instance.classId):
            return HttpWrite.infoFailure(request,'主题不存在!')
        return super(ThemeViewHelper,self).show_response(request,instance,dict,url)
    def show(self,request,id):
        return super(ThemeViewHelper,self).show(request,id)

    def addTheme_Extcredit(self,request):
        addTheme_Extcredit(request,request.user,'add')
    def addReply_Extcredit(self,request):
        addReply_Extcredit(request,request.user,'add')